﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TerminationCall.Core;

namespace TerminationCall.Editor.Controls {

    public partial class TileSetGroupedAutoborderView : UserControl {

        public event EventHandler<EventArgs> TileSelectionChange;

        private TileSet _tileSet;

        private Bitmap _mainDrawBitmap;
        private Graphics _mainDrawGraphic;
        private Graphics _panelGraphic;

        private Point _selection;

        public TileSet TileSet {
            get {
                return _tileSet;
            }
            set {
                _tileSet = value;
                _selection = Point.Empty;
                RenderTileSet();
            }
        }

        public Tile SelectedTile {
            get {
                int renderTileX = 0;
                int renderTileY = 0;
                for (int y = 0; y < 20; y++) {
                    for (int x = 0; x < 20; x++) {

                        if (_tileSet.Tiles[x, y].ChildTileField.Tiles.Length > 0) {

                            if (renderTileX == _selection.X && renderTileY == _selection.Y) {
                                return _tileSet.Tiles[x, y];
                            }

                            renderTileX++;

                            if (renderTileX == 19) {
                                renderTileY++;
                                renderTileX = 0;
                            }
                        }
                    }
                }

                return null;
            }
        }

        public TileSetGroupedAutoborderView() {
            
            InitializeComponent();

            _mainDrawBitmap = new Bitmap(400, 320);
            _mainDrawGraphic = Graphics.FromImage(_mainDrawBitmap);
            _panelGraphic = this.CreateGraphics();

        }

        private void RenderTileSet() {

            int renderTileX = 0;
            int renderTileY = 0;

            if (_tileSet == null) {
                return;
            }

            _mainDrawGraphic.Clear(Color.White);

            for (int y = 0; y < 20; y++) {
                for (int x = 0; x < 20; x++) {

                    if (_tileSet.Tiles[x, y].ChildTileField.Tiles.Length > 0) {

                        _mainDrawGraphic.DrawImage(_tileSet.Tiles[x, y].Bitmap, new Point(renderTileX * TileConstants.TileSizeX, renderTileY * TileConstants.TileSizeY));

                        renderTileX++;

                        if (renderTileX == 19) {
                            renderTileY++;
                            renderTileX = 0;
                        }
                    }
                }
            }

            for (int x = 0; x < 20; x++) {
                _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(50, 0, 0, 0)), x * TileConstants.TileSizeX, 0, x * TileConstants.TileSizeX, 320);
            }
            for (int y = 0; y < 20; y++) {
                _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(50, 0, 0, 0)), 0, y * TileConstants.TileSizeY, 481, y * TileConstants.TileSizeY);
            }

            int choosenTileX = _selection.X * TileConstants.TileSizeX;
            int choosenTileY = _selection.Y * TileConstants.TileSizeY;

            _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0, 0)), choosenTileX, choosenTileY, choosenTileX + TileConstants.TileSizeX, choosenTileY);
            _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0, 0)), choosenTileX, choosenTileY + TileConstants.TileSizeY, choosenTileX + TileConstants.TileSizeX, choosenTileY + TileConstants.TileSizeY);
            _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0, 0)), choosenTileX, choosenTileY, choosenTileX, choosenTileY + TileConstants.TileSizeY);
            _mainDrawGraphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0, 0)), choosenTileX + TileConstants.TileSizeX, choosenTileY, choosenTileX + TileConstants.TileSizeX, choosenTileY + TileConstants.TileSizeY);

            _panelGraphic.DrawImage(_mainDrawBitmap, new Point());
        }

        private void TileSetGroupedAutoborderView_MouseDown(object sender, MouseEventArgs e) {

            int x = e.X / TileConstants.TileSizeX;
            int y = e.Y / TileConstants.TileSizeY;

            _selection = new Point(x, y);

            RenderTileSet();

            if (TileSelectionChange != null) {
                TileSelectionChange(this, EventArgs.Empty);
            }

        }

        private void TileSetGroupedAutoborderView_Paint(object sender, PaintEventArgs e) {
            RenderTileSet();
        }

    }

}
